package com.weixin.gong.example.apache.commons.beanutils;

import org.apache.commons.beanutils.BeanUtils;
import org.junit.Assert;
import org.junit.Test;

import java.lang.reflect.InvocationTargetException;

/**
 * @author weixin.gong
 * @date 17-5-10 下午3:28
 */
public class BeanToBeanExample {
    public static class Bean1 {
        private int value;

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return "Bean1{" +
                    "value=" + value +
                    '}';
        }
    }

    public static class Bean2 {
        private String value;

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return "Bean2{" +
                    "value='" + value + '\'' +
                    '}';
        }
    }

    @Test
    public void beanToBean() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        Bean1 bean1 = new Bean1();
        bean1.setValue(1);
        Bean2 bean2 = new Bean2();
        BeanUtils.copyProperties(bean2, bean1);
        System.out.println("BeanToBean with BeanUtils:" + bean2);
        Assert.assertEquals(bean1.getValue() + "", bean2.getValue());
        //PropertyUtils.copyProperties需要保证字段类型一致
        //PropertyUtils.copyProperties(bean2, bean1);
        //System.out.println("BeanToBean with PropertyUtils:" + bean2);
        //Assert.assertEquals(bean1.getValue() + "", bean2.getValue());
    }
}
